package com.ruoyi.system.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.email.model.ToEmail;
import com.ruoyi.common.enums.EmailEnums;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.RandomUtils;
import com.ruoyi.common.utils.redis.RedisUtils;
import com.ruoyi.system.service.EmailUtilsServer;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSendException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName EmailUtilsServer
 * @Description
 * @Author wed
 * @Date 2022/3/25
 * @Version 1.0
 **/

@RequiredArgsConstructor
@Service
public class EmailUtilsServerImpl implements EmailUtilsServer {

    private final JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

    /**
     * 发送普通邮件
     *
     * @param toEmail
     * @return
     */
    @Override
    public Boolean commonEmail(ToEmail toEmail) {
        //创建简单邮件消息
        SimpleMailMessage message = new SimpleMailMessage();
        //谁发的
        message.setFrom(from);
        //谁要接收
        message.setTo(toEmail.getTos());
        //邮件标题
        message.setSubject(toEmail.getSubject());
        //邮件内容
        message.setText(toEmail.getContent());
        try {
            mailSender.send(message);
            return true;
        }catch (MailSendException e){
            e.getMessage();
            if (e.getMessageExceptions()[0].getMessage().equals("Invalid Addresses")){
                throw new ServiceException("邮箱地址无效！");
            }
            throw new ServiceException("发送邮件失败！");
        } catch (MailException e) {
            e.printStackTrace();
            throw new ServiceException("发送邮件未知错误，请联系管理员！");
        }
    }

    /**
     * 发送验证邮件
     *
     * @param email
     * @param enums
     * @return
     */
    @Override
    public Map<String, Object> CheckToEmail(String email, EmailEnums enums) {

        // 保存验证码信息
        String uuid = IdUtil.simpleUUID();
        String verifyKey = enums.getRedisKey() + uuid;

        // 生成验证码
        String code = RandomUtils.getStringRandom(6);
        RedisUtils.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);

        // 发送邮件
        ToEmail toEmail = new ToEmail();
        toEmail.setContent(code);
        toEmail.setTos(new String[]{email});
        toEmail.setSubject(enums.getValue());
        Boolean checkToEmail = commonEmail(toEmail);

        if (!checkToEmail) {
            return null;
        }

        Map<String, Object> ajax = new HashMap<>();
        ajax.put("uuid", uuid);
        ajax.put("code", "");
        return ajax;
    }
}
